package com.adms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adms.domain.Circular;
import com.adms.domain.CircularMapper;
import com.adms.domain.CircularReply;
import com.adms.domain.CircularReplyExample;
import com.adms.domain.CircularReplyExample.Criteria;
import com.adms.domain.CircularReplyMapper;
import com.adms.domain.Message;
import com.adms.domain.MessageMapper;
import com.adms.util.AdmsConfig;
import com.adms.util.ShiroUtil;
import com.adms.websocket.WebSocketServer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;

@Transactional
@Service
public class CircularReplyService {

	@Autowired
	private CircularReplyMapper circularReplyMapper;
	@Autowired
	private MessageMapper messageMapper;
	@Autowired
	private WebSocketServer webSocketServer;
	@Autowired
	private CircularMapper circularMapper;

	public CircularReply getByReplyId(Integer replyId) {
		return circularReplyMapper.selectByPrimaryKey(replyId);
	}

	public PageInfo<CircularReply> list(CircularReply circularReply, Boolean isSelf, String timeField, Date start, Date end,
			String orderByClause, Integer pageNum, Integer pageSize, Integer navigatePages) {
		CircularReplyExample ex = constructExample(circularReply, isSelf, timeField, start, end, orderByClause);
		
		PageHelper.startPage(pageNum, pageSize);
		List<CircularReply> replys = circularReplyMapper.selectByExample(ex);
		return new PageInfo<>(replys, navigatePages);
	}
	
	private CircularReplyExample constructExample(CircularReply circularReply, Boolean isSelf, String timeField, Date start, Date end,
			String orderByClause) {
		CircularReplyExample ex = new CircularReplyExample();
		Criteria or = ex.or();
		if(circularReply != null) {
			Integer ciruclarId = circularReply.getCircularId();
			Integer userId = circularReply.getUserId();
			Integer auditState = circularReply.getAuditState();
			if(ciruclarId != null) {
				or.andCircularIdEqualTo(ciruclarId);
			}
			if(userId != null) {
				or.andUserIdEqualTo(userId);
			}
			if(auditState != null) {
				or.andAuditStateEqualTo(auditState);
			}
		}
		
		if(isSelf != null && isSelf) {
			Integer userId = ShiroUtil.getUser().getUserId();
			or.andUserIdEqualTo(userId);
		}
		
		if(StringUtil.isNotEmpty(timeField)) {
			if(timeField.equals("createTime")) {
				if(start != null) {
					if(end != null) {
						or.andCreateTimeBetween(start, end);
					}else {
						or.andCreateTimeGreaterThanOrEqualTo(start);
					}
				}else {
					if(end != null) {
						or.andCreateTimeLessThanOrEqualTo(end);
					}
				}
			}
		}
		
		if(StringUtil.isNotEmpty(orderByClause)) {
			ex.setOrderByClause(orderByClause);
		}else {
			ex.setOrderByClause("circular_reply.create_time desc");
		}
		return ex;
	}

	public boolean audit(Integer replyId, Integer auditState) {
		if(auditState == 2) {
			CircularReply circularReply = circularReplyMapper.selectByPrimaryKey(replyId);
			if(circularReply != null) {
				circularReply.setAuditState(auditState);
				circularReplyMapper.updateByPrimaryKeySelective(circularReply);
				//新增站内信
				Circular circular = circularMapper.selectByPrimaryKey(circularReply.getCircularId());
				Message message = new Message();
				message.setCircularId(circularReply.getCircularId());
				message.setType(AdmsConfig.MESSAGE_TYPE_REJECTCIRCULARREPLY);
				message.setTitle(circular.getTitle());
				message.setUserId(circularReply.getUserId());
				messageMapper.insertSelective(message);
				// 发送webSocket消息
				webSocketServer.sendMessage(circularReply.getUserId(), message);
			}
		}
		return true;
	}
	
}
